package com.ems.util;


import springfox.documentation.swagger.common.XForwardPrefixPathAdjuster;
import sun.net.www.ParseUtil;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 22:24 2024-04-15
 * @return
 */

public class Test {

    // 0501
    // 03
    // 0000
    // 00000001
    // 0001
    // 00000002
    // 0003
    // 00000003

    // 0501
    // 03
    // 0000
    // 00
    // 0001
    // 02
    // 0003
    // 03

//    public static void main(String[] args) {
//        float floatValue = 123; //c2f60000   42f60000
//        int intBits = Float.floatToIntBits(floatValue);
//        String hexString = Integer.toHexString(intBits);
//        System.out.println("十六进制表示: " + hexString);
//    }

    public static void main (String[] args) {
        String number = "123"; // 示例的负数 0000007b
        Integer n = Integer.parseInt ( number );
        String hexString = Integer.toHexString ( n );
        System.out.println ( "十六进制表示: " + hexString );

        System.out.println ( "---" );
        String hexString1 = "fffffe"; // 示例十六进制字符串
        byte[] byteArray = HexUtil.hexStringToByteArray ( hexString );
        // 输出byte数组内容
        for ( byte b : byteArray ) {
            System.out.format ( "0x%X %d; ", b, b );
        }
    }

//    private static byte[] hexStringToByteArray (String s) {
//        System.out.println (s);
//        int len = s.length ();
//        // 如果不足8位，高位补0
//        while ( len < 8 ) {
//            System.out.println ("-----11");
//            s = "0" + s;
//            len++;
//        }
//        System.out.println (s);
//        System.out.println (len);
//        byte[] data = new byte[len / 2];
//        for ( int i = 0; i < len; i += 2 ) {
//            data[i / 2] = (byte) ( ( Character.digit ( s.charAt ( i ), 16 ) << 4 )
//                    + Character.digit ( s.charAt ( i + 1 ), 16 ) );
//        }
//        return data;
//    }


    /**
     * 检查一帧报文是否符合格式要求
     * @param frame
     * @return
     */
    public static boolean checkFrame (String frame) {
        String start = HexUtil.parseData ( frame, 0, 1 );//0c
        int length = HexUtil.hexToDecimal ( frame, 1, 2 ) + 8;
        if ( "0c".equals ( start ) && length == frame.length () / 2 ) {
            return true;
        }
        return false;
    }

    /**
     * 判断部分帧数据
     * @param notTailData：数组里没有尾巴的数据
     * @param notHeadData：第二帧没有头的数据
     * @return
     */
    public static String concatData (List<String> notTailData, String notHeadData) {
        for ( String notTailDatum : notTailData ) {
            String start = HexUtil.parseData ( notTailDatum, 0, 1 );//0c
            int frameLen = HexUtil.hexToDecimal ( notTailDatum, 1, 2 ) + 8;
            String data = notTailDatum.concat ( notHeadData ); // 有效数据
            if ( notTailDatum.startsWith ( start ) && data.length () / 2 == frameLen ) {
                return data;
            }
        }
        return null;
    }


    public static void action (String data, String commandCode) {
        //先拿总包数和第几包
        int countFrame = HexUtil.hexToDecimal ( data, 0, 1 );
        int currentFrame = HexUtil.hexToDecimal ( data, 1, 1 );
        System.out.println ( "总帧:" + countFrame );
        System.out.println ( "当前帧:" + currentFrame );
        //1.拿到每帧数据的长度
        int number = HexUtil.hexToDecimal ( data, 2, 1 );
        System.out.println ( "共" + number + "组" );
        //截取数目之后的遥测数据的所有字节
        String substring = data.substring ( 6 );
        System.out.println ( "有效数据:" + substring );
        //2.拿每组的数据，根据电表入库
        for ( int i = 0; i < number; i++ ) {
            //测点ID，两个字节
            //000000012f84
            //000100010428
            int index = 6;  // 索引
            int length = 4; // 长度
            index = commandCode.equals ( "08" ) ? 3 : 6;
            length = commandCode.equals ( "08" ) ? 1 : 4;
            int pointId = HexUtil.hexToDecimal ( substring, i * index, 2 );
            System.out.println ( pointId );
            //测点值，四个字节
            int value = HexUtil.hexToDecimal ( substring, i * index + 2, length );
            System.out.println ( value );
        }
    }

}
